capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local log "";
local do "";
local crime "";
local data "";
local output "";
local zip "" ;
local earn "";
local parearn "";
local paroth "";

/* 
Main Prep 3, variables defined by age (exactage)
*/


local exp "prworard";
local range "18(3)18" ; 
local firstyr 1997 ;	
local lastyr 2019 ;		
local firstage 18 ;
local lastage 40 ;
local lastearnyr 2019 ;

/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

local first 1996 ;
local last 2004 ;
local per_range "20" ;
local thresh 0.8 ; 	
include "`do'\crime_statecntyselect.do" ;

use "`crime'\prep2\crime_prwora_remerge.dta", clear;

/* Diagnosis */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);
replace n_diag1cat = 999 if n_diag1cat == . ;

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry */
tab n_firstage, m;
replace n_firstage = 999 if n_firstage == . ;
tab n_firstage, m ;

/* Parent earnings and household income */
local earncut = 5000 ;

order n_hhearns1996_*  n_combpay_* n_hhmbayr1996_* n_hhcombpay1996_* n_sibpayall_* ;
forval yr=1990(1)2012 { ;
	gen n_inchh1996_`yr' = n_hhearns1996_`yr' + n_combpay_`yr' + n_hhmbayr1996_`yr' + n_hhcombpay1996_`yr' + n_sibpayall_`yr' if n_hhearns1996_`yr' != . ;
} ;
forval yr=1990(1)2012 { ;
	gen n_inchh_`yr' = n_hhearns_`yr' + n_combpay_`yr' + n_hhmbayr_`yr' + n_hhcombpay_`yr' + n_sibpayall_`yr' if n_hhearns_`yr' != . ;
} ;
summ n_inchh* ;

egen n_earnhh9702=rowmean(n_hhearns_1997-n_hhearns_2002) ;
egen n_earnhh9712=rowmean(n_hhearns_1997-n_hhearns_2012) ;
egen n_earnhhpre9095=rowmean(n_hhearns_1990-n_hhearns_1995) ;

egen n_inchh9702=rowmean(n_inchh_1997-n_inchh_2002) ;
egen n_inchh9712=rowmean(n_inchh_1997-n_inchh_2012) ;
egen n_inchhpre9095=rowmean(n_inchh_1990-n_inchh_1995) ;

forval yr=1990(1)2012 { ;
	gen byte temp`yr' = (n_hhearns_`yr' > `earncut') ;
	replace temp`yr' = . if n_hhearns_`yr' == . ;
} ;
egen n_emphhpost9702=rowmean(temp1997-temp2002) ;
egen n_emphhpost9712=rowmean(temp1997-temp2012) ;
egen n_emphhpre9095=rowmean(temp1990-temp1995) ;
drop temp* ;

xtile n_earnhhbins = n_earnhhpre9095, nq(10) ;
tab n_earnhhbins, m ;
replace n_earnhhbins = 99 if n_earnhhbins == . ;
tab n_earnhhbins, m ;

/* First stage variables */
gen byte n_age18unfav=(age18_final_con==0);
egen n_onssi1922 = rowmean(n_onssi_1997-n_onssi_2000) ;

gen yob = year(dob) ;
summ yob ;
local yob_min = r(min) ;
local yob_max = r(max) ;
gen mob = month(dob) ;

forval yr = 1995(1)2003 { ;
	forval mo = 1(1)12 { ;
		gen byte n_onssimo_`yr'_`mo' = (combpmt_`yr'_`mo' > 0 & combpmt_`yr'_`mo' !=.) ;
	} ;
} ;

gen n_onssi1922_exact = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob =1(1)12 { ;
		egen temp_`yob'_`mob' = rowmean(n_onssimo_`=`yob'+19'_`=`mob''-n_onssimo_`=`yob'+22'_`=`mob'') if yob == `yob' & mob == `mob' ;
		replace n_onssi1922_exact = temp_`yob'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

gen n_onssi20 = . ;
forval yob = `yob_min'(1)`yob_max' { ;
	forval mob = 1(1)12 { ;
		replace n_onssi20 = n_onssimo_`=`yob'+20'_`mob' if yob == `yob' & mob == `mob' ;
	} ;
} ;

drop yob mob temp* ;
summ n_age18unfav n_onssi1922 n_onssi1922_exact n_onssi20 ;

/* Mortality */
gen numi_dob_mo = substr(numi_dob,1,2) ;
	replace numi_dob_mo = "" if numi_dob_mo == "00" | numi_dob_mo == "XX" ;
gen numi_dob_dd = substr(numi_dob,3,2) ;
	replace numi_dob_dd = "" if numi_dob_dd == "00" | numi_dob_dd == "XX" ;
gen numi_dob_yy = substr(numi_dob,5,4) ;
destring numi_dob_mo, replace ;
destring numi_dob_dd, replace ;
destring numi_dob_yy, replace ;
drop numi_dob ;
gen numi_dob = mdy(numi_dob_mo, numi_dob_dd, numi_dob_yy) ;

forval i = 1(1)3 { ;
	gen numi_dod`i'_mo = substr(numi_dod`i',1,2) ;
	gen numi_dod`i'_dd = substr(numi_dod`i',3,2) ;
		replace numi_dod`i'_dd = "" if numi_dod`i'_dd == "00" | numi_dod`i'_dd == "XX" ;
	gen numi_dod`i'_yy = substr(numi_dod`i',5,4) ;
	destring numi_dod`i'_mo, replace ;
	destring numi_dod`i'_dd, replace ;
	destring numi_dod`i'_yy, replace ;
	drop numi_dod`i' ;
	gen numi_dod`i' = mdy(numi_dod`i'_mo, numi_dod`i'_dd, numi_dod`i'_yy) ;
} ;
gen numi_dod = numi_dod1 ;
replace numi_dod = numi_dod2 if numi_dod == . & numi_dod2 != . ;
replace numi_dod = numi_dod3 if numi_dod == . & numi_dod3 != . ;
drop numi_dod1* numi_dod2* numi_dod3* ;
gen numi_yod = year(numi_dod) ;
gen byte numi_dthpost = (numi_yod > 1996 & numi_yod !=.) ;
tab numi_dthpost, m ;


/* ********************************* */
/* **** CREATE RUNNING VARIABLE **** */
/* ********************************* */
gen dobdd_stata	=mdy(month(dob),day(dob),year(dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);

gen dobwk_stata	=wofd(mdy(month(dob),day(dob),year(dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));

gen dobmo_stata	=ym(year(dob)+18,month(dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;


/* ************************** */
/* **** SAMPLE SELECTION **** */
/* ************************** */

keep if year(first_pay_dte)<1996 ;
count;


/* ********************************************************************** */
/* **** CREATE CRIME VARIABLES USING DIFFERENT GEO/YEAR DENOMINATORS **** */
/* ********************************************************************** */

rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_* cnty_*) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;


/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;
local hinc_cond 16 ;

local vcch_cond 1 ;
local vcgb_cond 2 ;
local vcpd_cond 3 ;
local vcpp_cond 4 ;
local vcps_cond 5 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;
local jail_abbr		"jail" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc  
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	vcch vcgb vcpd vcpp vcps
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char genr nogn drug viol prop pord fel fcvn acvn inca" ; 
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list_vice "vcch vcgb vcpd vcpp vcps" ;
local type_list "`type_list_main' `type_list_bk' `type_list_nobk' `type_list_vice'" ;

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;
foreach stub in `type_list_vice' { ;
	local `stub'_event "vice_event" ;
} ;


rename indiv_match temp1 ;

forval i=1(1)9 { ;
	rename event_date0`i' event_date`i' ;
	rename event0`i' event`i' ;
	rename day_of_week0`i' day_of_week`i' ;
	rename event_state0`i' event_state`i' ;
} ;

foreach class in bk nobk vice { ;
	forval i=1(1)9 { ;
		rename `class'_event_date0`i' 	`class'_event_date`i' ;
		rename `class'_event0`i' 		`class'_event`i' ;
		rename `class'_event_state0`i' 	`class'_event_state`i' ;
	} ;
} ;


/* **** PREP THAT IS SPECIFIC TO EXACT AGE VERSION **** */
gen dob_yy = year(dob) ;
local base = 1978 ;

forval age = 18(3)18 { ;			
	local ends_`age' " " ;
	forval int = 20(2)20 { ;			
		if `=`age'+`int'' <= `lastage' { ;
			local ends_`age' " `ends_`age'' `=`age'+`int'' " ;
		} ;
	} ;
	display `ends_`age'' ;
} ;


gen byte temp2 = (event_date1 != .) ;

forval i = 1(1)2 { ;
		gen n_crime`i'any_post = temp`i' if 	keep_`=`base'+`firstage''post_any == 1 ;
} ;
drop temp* ;

foreach class in event bk_event nobk_event vice_event { ;
	forval i=1(1)20 { ;
		recast double `class'`i' ;
		tostring(`class'`i'), replace format(%18.0g) ;
		forval j = 3(1)9 { ;
			replace `class'`i' = subinstr(`class'`i',"`j'","2",.) ;
		} ;
	} ;
} ;

forval yr = `firstage'(1)`lastage' { ;
	gen days_in_ipp`yr' = days_in_hprison`yr' + days_in_parole`yr' + days_in_probation`yr' ;
} ;

/* **** GENERIC **** */

forval start = `range' { ;
	foreach end in `ends_`start'' { ;
	
		/* Indicator */
		gen 		n_crime2any_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2any_`start'_`end' = 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
				& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') ;
			} ;
		
		forval yr=`start'(1)`end' { ;
			replace n_crime2any_`start'_`end' = 1 
				if n_crime2any_`start'_`end' != . 
				& days_in_ipp`yr' > 0 & days_in_ipp`yr' != . ;
		} ;
		
		/* Total number */
		gen 		n_crime2anyt_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2anyt_`start'_`end' = n_crime2anyt_`start'_`end' + 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
				& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2any`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2any`yr'_`start'_`end' = 1
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr') ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2anyt`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2anyt`yr'_`start'_`end' = c_crime2anyt`yr'_`start'_`end' + 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr') ;
				} ;
			} ;
				
	} ;	/* end end */
} ;	/* end start */

/* heterogeneity by pre-18 charges */
gen 		n_crime2any_pre18= 0 
		if (keep_`=`base'+18'_`=`base'+38'_any == 1) ;
forval i=1(1)20 { ;
	replace n_crime2any_pre18 = 1 
		if (keep_`=`base'+18'_`=`base'+38'_any == 1)
		& event_date`i' < mdy(dob_mm, dob_dd, dob_yy + 18) ;
	} ;


/* **** SPECIFIC TYPES OF CRIME **** */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `ends_`start'' { ;
			
			/* Indicator */
			gen 		n_crime2`type'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type'_`start'_`end' = 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
					& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			/* Total number */
			gen 		n_crime2`type't_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type't_`start'_`end' = n_crime2`type't_`start'_`end' + 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
					& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */


/* Special combinations */
forval start = `range' { ;
	foreach end in `ends_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2vnrb_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrb_`start'_`end' = 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1)
				& `viol_event'_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & `viol_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end')  
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
		/* Total number */
		gen 		n_crime2vnrbt_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrbt_`start'_`end' = n_crime2vnrbt_`start'_`end' + 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1)
				& `viol_event'_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & `viol_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
	} ;	/* end end */
} ;	/* end start */

	
forval start = `range' { ;
	foreach end in `ends_`start'' { ;
		
	egen n_crime2dgpo_`start'_`end'= rowmax(n_crime2dgps_`start'_`end' n_crime2dgot_`start'_`end')	 
		if n_crime2dgps_`start'_`end' != . & n_crime2dgot_`start'_`end' != . ;
	
	egen n_crime2dgpot_`start'_`end'= rowtotal(n_crime2dgpst_`start'_`end' n_crime2dgott_`start'_`end') 
		if n_crime2dgpst_`start'_`end' != . & n_crime2dgott_`start'_`end' != ., missing ;
		
	egen n_crime2nibd_`start'_`end'= rowmax(n_crime2nidc_`start'_`end' n_crime2niob_`start'_`end')	 
		if n_crime2nidc_`start'_`end' != . & n_crime2niob_`start'_`end' != . ;
	
	egen n_crime2nibdt_`start'_`end'= rowtotal(n_crime2nidct_`start'_`end' n_crime2niobt_`start'_`end') 
		if n_crime2nidct_`start'_`end' != . & n_crime2niobt_`start'_`end' != ., missing ;
		
	gen n_crime2sxdg_`start'_`end' = (n_crime2nisx_`start'_`end' == 1 &  n_crime2drug_`start'_`end' == 1)
		if n_crime2nisx_`start'_`end' != . &  n_crime2drug_`start'_`end' != . ;
		
	gen n_crime2sxvc_`start'_`end' = (n_crime2nisx_`start'_`end' == 1 &  n_crime2povc_`start'_`end' == 1)
		if n_crime2nisx_`start'_`end' != . &  n_crime2povc_`start'_`end' != . ;
		
	gen n_crime2gnvl_`start'_`end' = (n_crime2genr_`start'_`end' == 1 &  n_crime2vnrb_`start'_`end' == 1)
		if n_crime2genr_`start'_`end' != . &  n_crime2vnrb_`start'_`end' != . ;
		
	gen n_crime2vcot_`start'_`end' = (n_crime2povc_`start'_`end' == 1 
										&  n_crime2vcch_`start'_`end' != 1
										&  n_crime2vcgb_`start'_`end' != 1
										&  n_crime2vcpd_`start'_`end' != 1
										&  n_crime2vcpp_`start'_`end' != 1
										&  n_crime2vcps_`start'_`end' != 1
										)
		if n_crime2povc_`start'_`end' != . ;
		
	/* anti-social index (disorderly conduct, non-rob violent) */
	egen n_crime2anti_`start'_`end'= rowmax(n_crime2nidc_`start'_`end' n_crime2vnrb_`start'_`end')	 
		if n_crime2nidc_`start'_`end' != . & n_crime2vnrb_`start'_`end' != . ;
	
	egen n_crime2antit_`start'_`end'= rowtotal(n_crime2nidct_`start'_`end' n_crime2vnrbt_`start'_`end') 
		if n_crime2nidct_`start'_`end' != . & n_crime2vnrbt_`start'_`end' != ., missing ;
		
	/* income-requiring charge estimates (alcohol, drug, with and without weapons) */
	egen n_crime2req1_`start'_`end'= rowmax(n_crime2nidp_`start'_`end' n_crime2nidu_`start'_`end' n_crime2nilq_`start'_`end' n_crime2niwp_`start'_`end')	 
		if n_crime2nidp_`start'_`end' != . & n_crime2nidu_`start'_`end' != . & n_crime2nilq_`start'_`end' != . & n_crime2niwp_`start'_`end' != . ;
	
	egen n_crime2req1t_`start'_`end'= rowtotal(n_crime2nidpt_`start'_`end' n_crime2nidut_`start'_`end' n_crime2nilqt_`start'_`end' n_crime2niwpt_`start'_`end') 
		if n_crime2nidpt_`start'_`end' != . & n_crime2nidut_`start'_`end' != . & n_crime2nilqt_`start'_`end' != . & n_crime2niwpt_`start'_`end' != ., missing ;
		
	egen n_crime2req2_`start'_`end'= rowmax(n_crime2nidp_`start'_`end' n_crime2nidu_`start'_`end' n_crime2nilq_`start'_`end')	 
		if n_crime2nidp_`start'_`end' != . & n_crime2nidu_`start'_`end' != . & n_crime2nilq_`start'_`end' != . ;
	
	egen n_crime2req2t_`start'_`end'= rowtotal(n_crime2nidpt_`start'_`end' n_crime2nidut_`start'_`end' n_crime2nilqt_`start'_`end') 
		if n_crime2nidpt_`start'_`end' != . & n_crime2nidut_`start'_`end' != . & n_crime2nilqt_`start'_`end' != ., missing ;
		
	} ;	/* end end */
} ;	/* end start */

/* Timing of income-generating and violent */
forval start = `range' { ;
	foreach end in `ends_`start'' { ;
	
		foreach type in viol genr { ;
			gen temp_first_`type'_`start'_`end' = . ;
			forval i=1(1)20 { ;
					replace temp_first_`type'_`start'_`end' = ``type'_event'_date`i' 
					if substr(``type'_event'`i',``type'_cond',1) == "2" 
					& ``type'_event'_date`i'  >= mdy(dob_mm, dob_dd, dob_yy + `start') & ``type'_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') ;
			} ;
		} ;
		
	gen 		n_crime2vtheng_`start'_`end'= 0 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1) ;
	replace 	n_crime2vtheng_`start'_`end'= 1 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1)
				& temp_first_viol_`start'_`end' != . & temp_first_genr_`start'_`end' != . & temp_first_viol_`start'_`end' + 30 < temp_first_genr_`start'_`end' 
			;
			
	gen 		n_crime2gthenv_`start'_`end'= 0 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1) ;
	replace 	n_crime2gthenv_`start'_`end'= 1 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1)
				& temp_first_viol_`start'_`end' != . & temp_first_genr_`start'_`end' != . & temp_first_genr_`start'_`end' + 30 < temp_first_viol_`start'_`end'
			;
			
	gen 		n_crime2gvsame_`start'_`end'= 0 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1) ;
	replace 	n_crime2gvsame_`start'_`end'= 1 
			if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`genr_abbr' == 1)
				& temp_first_viol_`start'_`end' != . & temp_first_genr_`start'_`end' != . & abs(temp_first_genr_`start'_`end' - temp_first_viol_`start'_`end') <= 30  
			;
	
	} ;	/* end end */
} ;	/* end start */	
drop temp_* ;
	
	
/* cumulative */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `ends_`start'' { ;
			
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type'`yr'_`start'_`end'= 0 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type'`yr'_`start'_`end' = 1 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
						& ``type'_event'_date`i'  >= mdy(dob_mm, dob_dd, dob_yy + `start') & ``type'_event'_date`i'  <= mdy(dob_mm, dob_dd, dob_yy + `yr') 
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type't`yr'_`start'_`end'= 0 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type't`yr'_`start'_`end' = c_crime2`type't`yr'_`start'_`end' + 1 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
						& ``type'_event'_date`i'  >= mdy(dob_mm, dob_dd, dob_yy + `start') & ``type'_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr')
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */

forval start = `range' { ;
	foreach end in `ends_`start'' { ;
		forval yr = `=`end'-(4*4)'(4)`end' { ;
		
		egen c_crime2dgpo`yr'_`start'_`end' = rowmax(c_crime2dgps`yr'_`start'_`end' c_crime2dgot`yr'_`start'_`end')	 
			if c_crime2dgps`yr'_`start'_`end' != . & c_crime2dgot`yr'_`start'_`end' != . ;
		
		egen c_crime2dgpot`yr'_`start'_`end' = rowtotal(c_crime2dgpst`yr'_`start'_`end' c_crime2dgott`yr'_`start'_`end') 
			if c_crime2dgpst`yr'_`start'_`end' != . & c_crime2dgott`yr'_`start'_`end' != ., missing ;
			
		egen c_crime2nibd`yr'_`start'_`end' = rowmax(c_crime2nidc`yr'_`start'_`end' c_crime2niob`yr'_`start'_`end')	 
			if c_crime2nidc`yr'_`start'_`end' != . & c_crime2niob`yr'_`start'_`end' != . ;
		
		egen c_crime2nibdt`yr'_`start'_`end' = rowtotal(c_crime2nidct`yr'_`start'_`end' c_crime2niobt`yr'_`start'_`end') 
			if c_crime2nidct`yr'_`start'_`end' != . & c_crime2niobt`yr'_`start'_`end' != ., missing ;
			
		} ; /* end yr */
	} ;	/* end end */
} ;	/* end start */

forval start = `range' { ;
	foreach end in `ends_`start'' { ;
		
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2vnrb`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2vnrb`yr'_`start'_`end' = 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1)
					& `viol_event'_date`i'  >= mdy(dob_mm, dob_dd, dob_yy + `start') & `viol_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr')
					& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2vnrbt`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2vnrbt`yr'_`start'_`end' = c_crime2vnrbt`yr'_`start'_`end' + 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_`viol_abbr' == 1 & keep_`=`base'+`start''_`=`base'+`end''_`vrob_abbr' == 1)
					& `viol_event'_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & `viol_event'_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr') 
					& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
				} ;
			} ;
			
		} ;	/* end end */
	} ;	/* end start */

		
		


/* Incarceration, parole, or probation FLOW */

forval start = `range' { ;
	foreach end in `ends_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2ipp_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ipp_`start'_`end' = 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1 )
				& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
		/* Total number */
		gen 		n_crime2ippt_`start'_`end'= 0 
				if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ippt_`start'_`end' = n_crime2ippt_`start'_`end' + 1 
				if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1 )
				& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ipp`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ipp`yr'_`start'_`end' = 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr') 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ippt`yr'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ippt`yr'_`start'_`end' = c_crime2ippt`yr'_`start'_`end' + 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_ipp == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `yr') 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
		
		} ;	/* end end */
} ;	/* end start */


/* Length of IPP spells / indicator for any IPP */
local full_hinc "hprison" ;
local full_inca "prison" ;
local full_par  "parole" ;
local full_prob "probation" ;
local full_ipp 	"ipp" ;
local full_jail "jail" ;

local correct_list "hinc jail par prob ipp " ; 

foreach type in `correct_list' { ;
	capture drop days_in_`full_`type''1997-days_in_`full_`type''2017 ;		
} ;
foreach type in `correct_list' { ;
	order days_in_`full_`type''*, alpha ;		
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
			foreach end in `ends_`start'' { ;
				egen 		n_crime2s`type'_`start'_`end' = rowmean(days_in_`full_`type''`start'-days_in_`full_`type''`end') if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1 ) ;	
				
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					egen c_crime2s`type'`yr'_`start'_`end' = rowtotal(days_in_`full_`type''`start'-days_in_`full_`type''`yr') if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstage'(1)`lastage' { ;
		gen n_yrcrime2s`type'_`i' = days_in_`full_`type''`i' if (keep_yr`=`base'+`i''_`type' == 1) ;
	} ;
} ;

foreach abbr in `correct_list' { ;
	forval age = `firstage'(1)`lastage' { ;
		gen byte any_`abbr'`age' = (days_in_`full_`abbr''`age' > 0) ;
		replace  any_`abbr'`age' = . if days_in_`full_`abbr''`age' == . ;
	} ;
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
			foreach end in `ends_`start'' { ;
			
				egen 		n_crime2a`type'_`start'_`end' = rowmean(any_`type'`start'-any_`type'`end') if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1 ) ;
				
				egen 		n_crime2c`type'_`start'_`end' = rowmax(any_`type'`start'-any_`type'`end') if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1 ) ;
				
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					egen c_crime2c`type'`yr'_`start'_`end' = rowmax(any_`type'`start'-any_`type'`yr') if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstage'(1)`lastage' { ;
		gen n_yrcrime2a`type'_`i' = any_`type'`i' if (keep_yr`=`base'+`i''_`type' == 1) ;
	} ;
} ;


/* **** WEEKDAY/WEEKEND **** */
forval i=1(1)20 { ;
	replace day_of_week`i' = 7 if day_of_week`i' == 0 ;
} ; 

local week_min 1 ;
local week_max 4 ;
local wknd_min 5 ;
local wknd_max 7 ;

local genr_first "g" ;
local char_first "c" ;
local inca_first "i" ;

foreach wk in week wknd { ;
	forval start = `range' { ;;
		foreach end in `ends_`start'' { ;
		
			/* Any type */
			gen 		n_crime2`wk'_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`wk'_`start'_`end' = 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
					& (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
				} ;
				
			gen 		n_crime2`wk't_`start'_`end'= 0 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`wk't_`start'_`end' = n_crime2`wk't_`start'_`end' + 1 
					if (keep_`=`base'+`start''_`=`base'+`end''_any == 1)
					& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
					& (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') ;
				} ;
				
			/* Specific crimes */
			foreach type in genr { ;
				gen 		n_crime2`wk'``type'_first'_`start'_`end'= 0 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace n_crime2`wk'``type'_first'_`start'_`end' = 1 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
						& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
						& (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') 
						& substr(event`i',``type'_cond',1) == "2" ;
					} ;
					
				gen 		n_crime2`wk'``type'_first't_`start'_`end'= 0 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace n_crime2`wk'``type'_first't_`start'_`end' = n_crime2`wk'``type'_first't_`start'_`end' + 1 
						if (keep_`=`base'+`start''_`=`base'+`end''_``type'_abbr' == 1)
						& event_date`i' >= mdy(dob_mm, dob_dd, dob_yy + `start') & event_date`i' <= mdy(dob_mm, dob_dd, dob_yy + `end') 
						& (day_of_week`i' >= ``wk'_min' & day_of_week`i' <= ``wk'_min') 
						& substr(event`i',``type'_cond',1) == "2" ;
					} ;
			} ;
				
		} ;	/* end end */
	} ;	/* end start */

} ; /*end wk */

/* decompose crimes into intensive and extensive */
local thresh_list "char genr" ;
local thresh_max = 20 ;

foreach stub in `thresh_list' {;

	/* Exact */
	forval i = 1(1)`thresh_max' { ;
		gen 	ct_exact_crime2`stub'`i' = 0 if  n_crime2`stub't_18_38 != . ;
		replace ct_exact_crime2`stub'`i' = 1 if (n_crime2`stub't_18_38 == `i' & n_crime2`stub't_18_38 !=.) ;
	} ;
	
	/* Greater or equal */
	forval i = 1(1)`thresh_max' { ;
		gen 	ct_gt_crime2`stub'`i' = 0 if  n_crime2`stub't_18_38 != . ;
		replace ct_gt_crime2`stub'`i' = 1 if (n_crime2`stub't_18_38 >  `i' & n_crime2`stub't_18_38 !=.) ;
	} ;
} ;
	

summ n_crime* ct_* ;

gen byte n_post=(dobdd_run>0);
tab n_post, m;

foreach stub in any 2c 6cg 7cn { ;
	rename keep_1997_2017_`stub' tkeep_1997_2017_`stub' ;
} ;

drop 
	first_name fssr_middlen last_name from_prwora from_name idu middle_name
	combpmt*
	n_combpay_* n_onssi_* n_sibpayall_* 
	n_hhearns1996_* n_hhmbayr1996_* n_hhcombpay1996_* n_inchh1996_* 
	n_hhearns_* n_hhmbayr_* n_hhcombpay_* n_inchh_* earn*
	days_in_* *event* *day_of_week* 
	keep_* i ;

foreach stub in any 2c 6cg 7cn { ;
	rename tkeep_1997_2017_`stub' keep_1997_2017_`stub' ;
} ;

save "`crime'\prep2\crime_prwora_prepforreg_exactage.dta", replace ;

capture log close;
